package com.homework.controller;

import com.homework.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/health")
public class HealthController {

    /**
     * 健康检查接口
     */
    @GetMapping
    public Result<Map<String, Object>> health(HttpServletRequest request) {
        log.info("收到健康检查请求 - 来源: {}, 时间: {}",
                request.getRemoteAddr(),
                LocalDateTime.now());

        Map<String, Object> info = new HashMap<>();
        info.put("status", "UP");
        info.put("timestamp", LocalDateTime.now());
        info.put("server", "Homework Management Backend");
        info.put("version", "1.0.0");
        info.put("port", 8765);
        info.put("clientIp", request.getRemoteAddr());
        info.put("userAgent", request.getHeader("User-Agent"));
        info.put("requestUrl", request.getRequestURL().toString());

        log.info("健康检查完成 - 返回信息: {}", info);
        return Result.success(info);
    }

    /**
     * 简单的ping接口
     */
    @GetMapping("/ping")
    public Result<String> ping() {
        log.info("收到ping请求");
        return Result.success("pong");
    }
}